﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using NLog;
using Medianamik.Core;
using Medianamik.Core.Logging;
using Medianamik.Core.Mail;
using System.Globalization;

namespace Medianamik.UI.Web
{
    public partial class SupportRequestWindow : BasePage
    {
        private static ILogger _logger;
        protected static ILogger Logger
        {
            get
            {
                return _logger
                    ?? (_logger = new ObjectFactory<ILogger>().Get("SupportRequestWindowLogger"));
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                MultiView.DataBind();
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            bool errorOccured = false;
            try
            {
                //Envoyer la demande de support
                new MailManager().SendTechSupportRequestEmail(UserName, 
                    UserMailAddress, txtMessage.Text, Context);

                //Envoyer le mail de confirmation
                new MailManager().SendTechSupportConfirmationEmail(UserName, 
                    UserMailAddress, txtMessage.Text, Context);
            }
            catch (Exception exc)
            {
                Logger.Error("Error while sending Support Request", exc);
                errorOccured = true;
            }
            if (errorOccured)
            {
                MultiView.SetActiveView(ViewRequestError);
            }
            else
            {
                MultiView.SetActiveView(ViewRequestSent);
            }
        }

        protected override bool AuthenticatedPage
        {
            get
            {

                return false; //La page de support doit être accessible depuis la page de login
            }
        }

        protected string UserMailAddress
        {
            get
            {
                if (User.Identity.IsAuthenticated)
                {
                    return MedianamikUser.Email;
                }
                else
                {
                    var txtUserMailAddress = FindControlRecursive(Page, "txtUserMailAddress") as TextBox;
                    return txtUserMailAddress == null ? string.Empty : txtUserMailAddress.Text;
                }
            }
        }

        protected string UserName
        {
            get
            {
                if (User.Identity.IsAuthenticated)
                {
                    return MedianamikUser.UserName;
                }
                else
                {
                    var txtUserName = FindControlRecursive(Page, "txtUserName") as TextBox;
                    //var txtUserName = Page.FindControl("txtUserName") as TextBox;
                    return txtUserName == null ? string.Empty : txtUserName.Text;
                }
            }
        }

        public Control FindControlRecursive(Control parentControl, string controlName)
        {
            Control controlSearched = parentControl.FindControl(controlName);
            if (controlSearched != null)
                return controlSearched;
            foreach(Control currentControl in parentControl.Controls)
            {
                controlSearched = FindControlRecursive(currentControl, controlName);
                if (controlSearched != null)
                    return controlSearched;
            }
            return null;
        }
    }
}
